package nix.hibernate.many;

import nix.domain.Customer;
import nix.domain.LinkMan;
import nix.utils.HibernateUtils;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

public class DemoOne2Many {

	@Test
	public void fun1() {		
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();
		// 创建瞬时状态对象
		Customer c = new Customer();
		c.setCust_name("世贸通");		
		LinkMan lm1 = new LinkMan();
		lm1.setLkm_name("张三");	
		LinkMan lm2 = new LinkMan();
		lm2.setLkm_name("李四");
		// 装配关系
		c.getLinkMens().add(lm1);
		c.getLinkMens().add(lm2);		
		lm1.setCustomer(c);
		lm2.setCustomer(c);
		// 持久化
		session.save(c);
		session.save(lm1);
		session.save(lm2);
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void fun2() {		
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();
		// 获取持久化对象
		Customer c = (Customer) session.get(Customer.class, 1L);
		// 创建瞬时状态对象
		LinkMan lm = new LinkMan();
		lm.setLkm_name("王五");
		// 转配关系
		c.getLinkMens().add(lm);
		lm.setCustomer(c);
		// 持久化
		session.save(lm);
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void fun3() {		
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();
		// 获取持久化对象
		Customer c = (Customer) session.get(Customer.class, 1L);
		// 获得要删除的持久化对象
		LinkMan lm = (LinkMan) session.get(LinkMan.class, 3L);
		// 移除关系
		c.getLinkMens().remove(lm);
		lm.setCustomer(null);
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void fun4() {		
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();
		// 创建瞬时状态对象
		Customer c = new Customer();
		c.setCust_name("世贸通");		
		LinkMan lm1 = new LinkMan();
		lm1.setLkm_name("张三");	
		LinkMan lm2 = new LinkMan();
		lm2.setLkm_name("李四");
		// 装配关系
		c.getLinkMens().add(lm1);
		c.getLinkMens().add(lm2);		
		lm1.setCustomer(c);
		lm2.setCustomer(c);
		// 持久化（级联保存）
		session.save(c);
		// session.save(lm1);
		// session.save(lm2);
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void fun5() {		
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();
		// 获取持久化对象
		Customer c = (Customer) session.get(Customer.class, 1L);
		// 级联删除
		session.delete(c);
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void fun6() {		
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();
		// 创建瞬时状态对象
		Customer c = new Customer();
		c.setCust_name("世贸通");		
		LinkMan lm1 = new LinkMan();
		lm1.setLkm_name("张三");	
		LinkMan lm2 = new LinkMan();
		lm2.setLkm_name("李四");
		// 装配关系（关系维护反转给多方）
		// c.getLinkMens().add(lm1);
		// c.getLinkMens().add(lm2);		
		lm1.setCustomer(c);
		lm2.setCustomer(c);
		// 持久化
		session.save(c);
		session.save(lm1);
		session.save(lm2);
		
		tx.commit();
		session.close();
	}
	
	@Test
	public void fun7() {		
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();

		Customer c = (Customer) session.get(Customer.class, 1L);
		System.out.println(c.getLinkMens());
		
		tx.commit();
		session.close();
	}
}
